
package Product;
import ConnnectData.DataConnect;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.modelTable;
/**
 *
 * @author langtustupid
 */
public class Product_show extends javax.swing.JInternalFrame {

    Connection Con;
    Statement Sta;
    ResultSet Res;
    public Product_show() {
        initComponents();
        try {
            Con = DataConnect.connectDB();
            Sta = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }
  

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField_Search = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbProduct = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnEdit = new javax.swing.JMenu();
        mnAdd = new javax.swing.JMenu();
        mnDelete = new javax.swing.JMenu();
        mnShow = new javax.swing.JMenu();
        mnClose = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Product Management");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24));
        jLabel1.setText("Product Management");

        jLabel2.setText("Product Name :");

        jTextField_Search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField_SearchKeyReleased(evt);
            }
        });

        tbProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbProduct.setRowHeight(20);
        tbProduct.setSelectionBackground(new java.awt.Color(255, 153, 255));
        jScrollPane1.setViewportView(tbProduct);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(198, 198, 198)
                .addComponent(jLabel2)
                .addGap(27, 27, 27)
                .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(434, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 903, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(280, 280, 280)
                .addComponent(jLabel1)
                .addContainerGap(416, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(39, 39, 39)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jMenuBar1.setBackground(new java.awt.Color(255, 255, 255));

        mnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/edit.JPG"))); // NOI18N
        mnEdit.setText("Edit Product");
        mnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnEditMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnEdit);

        mnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.gif"))); // NOI18N
        mnAdd.setText(" Add Product");
        mnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnAddMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnAdd);

        mnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/delete.png"))); // NOI18N
        mnDelete.setText("Delete Product ");
        mnDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnDeleteMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnDelete);

        mnShow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/star.gif"))); // NOI18N
        mnShow.setText("Show Product");
        mnShow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnShowMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnShow);

        mnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/exit.png"))); // NOI18N
        mnClose.setText("Close");
        mnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnCloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnClose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField_SearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_SearchKeyReleased
        try {
            Res = Sta.executeQuery("select Product.Product_Id,Product_Name,Product_Unit,Product_Price,Product_Quantity,Product_Description,convert(char(12),DateImport,101)as DateImport,Category_Name,Supplier_Name from((Product inner join Supplier on Product.Supplier_Id=Supplier.Supplier_Id)inner join Product_Category on Product_Category.Category_Id=Product.Category_Id)  where Product_Name like '%" + jTextField_Search.getText().trim() + "%' order by Product_Name asc");
            tbProduct.setModel(new modelTable(Res));
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }//GEN-LAST:event_jTextField_SearchKeyReleased

    private void mnDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnDeleteMouseClicked
       if (tbProduct.getSelectedRow() != -1) {
            int remove = JOptionPane.showConfirmDialog(this, "Are you sure want delete this Product !");
            if (remove == 0) {
                int index = Integer.parseInt(tbProduct.getValueAt(tbProduct.getSelectedRow(), 0).toString());
                try {
                    String sql_delFK = "Delete from Bill_Detail where Product_Id=" + index;
                    Sta.executeUpdate(sql_delFK);
                    String sql_del = "Delete from Product where Product_Id=" + index;
                    Sta.executeUpdate(sql_del);
                    Res = Sta.executeQuery("select Product.Product_Id,Product_Name,Product_Unit,Product_Price,Product_Quantity,Product_Description,convert(char(12),DateImport,101)as DateImport ,Category_Name,Supplier_Name from((Product inner join Supplier on Product.Supplier_Id=Supplier.Supplier_Id)inner join Product_Category on Product_Category.Category_Id=Product.Category_Id) order by Product_Name asc");
                    tbProduct.setModel(new modelTable(Res));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                
                JOptionPane.showMessageDialog(this, "Delete Product success ! ");
            }

        } else {
            JOptionPane.showMessageDialog(null, "You should choose the Product want to delete !");
        }
    }//GEN-LAST:event_mnDeleteMouseClicked

    private void mnShowMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnShowMouseClicked
    try {
            String sql="select Product.Product_Id,Product_Name,Product_Unit,Product_Price,Product_Quantity,"
                    + "Product_Description,convert(char(12),DateImport,101)as DateImport ,Product_Status,Category_Name,"
                    + "Supplier_Name from((Product inner join Supplier on Product.Supplier_Id=Supplier.Supplier_Id)"
                    + "inner join Product_Category on Product_Category.Category_Id=Product.Category_Id) "
                    + " where Product.Product_Status = 1 order by  Product.Product_Id, Product_Name asc";
            Res = Sta.executeQuery(sql);
            tbProduct.setModel(new modelTable(Res));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_mnShowMouseClicked

    private void mnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnAddMouseClicked
       new Add_Product_11().setVisible(true);
    }//GEN-LAST:event_mnAddMouseClicked

    private void mnEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnEditMouseClicked
        int rowindex = tbProduct.getSelectedRow();
        if(tbProduct.getSelectedRow()!=-1)
        {
        int Product_Id = Integer.parseInt(tbProduct.getValueAt(rowindex, 0).toString());
        String Product_Name =(tbProduct.getValueAt(rowindex, 1).toString());
        String Product_Unit =(tbProduct.getValueAt(rowindex, 2).toString());
        float Product_Price = Float.parseFloat(tbProduct.getValueAt(rowindex, 3).toString());
        int Product_Quantity = Integer.parseInt(tbProduct.getValueAt(rowindex, 4).toString());
        String Product_Description =(tbProduct.getValueAt(rowindex, 5).toString());
        String Quantity_Name =(tbProduct.getValueAt(rowindex, 6).toString());
        String Category_Name =(tbProduct.getValueAt(rowindex, 7).toString());
        String Supplier_Name =(tbProduct.getValueAt(rowindex, 8).toString());
        Update_Product frmUpdateProduct = new Update_Product();
        frmUpdateProduct.setProduct_Id(Product_Id);
        frmUpdateProduct.setProduct_Name(Product_Name);
        frmUpdateProduct.setProduct_Unit(Product_Unit);
        frmUpdateProduct.setProduct_Price(Product_Price);
        frmUpdateProduct.setProduct_Quantity(Product_Quantity);
        frmUpdateProduct.setProduct_Description(Product_Description);
        frmUpdateProduct.setCategory_Name(Category_Name);
        frmUpdateProduct.setSupplier_Name(Supplier_Name);
        frmUpdateProduct.setVisible(true);
        JOptionPane.showMessageDialog(this, "Update Curved");
        }
        else
        { JOptionPane.showMessageDialog(null, "You should choose the Product want to edit ! !");}
  

    }//GEN-LAST:event_mnEditMouseClicked

private void mnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnCloseMouseClicked
// TODO add your handling code here:
    int Close=JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
       if(Close==0)
       this.dispose();
}//GEN-LAST:event_mnCloseMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Product_show().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField_Search;
    private javax.swing.JMenu mnAdd;
    private javax.swing.JMenu mnClose;
    private javax.swing.JMenu mnDelete;
    private javax.swing.JMenu mnEdit;
    private javax.swing.JMenu mnShow;
    private javax.swing.JTable tbProduct;
    // End of variables declaration//GEN-END:variables

}
